package com.paas.form.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bpm.feign.model.ProcessCmd;
import com.bpm.feign.service.IBpmProcessInstanceService;
import com.paas.common.model.RestResponse;
import com.paas.common.util.UniqueIdUtil;
import com.paas.form.dto.FormViewPubChangeDto;
import com.paas.form.entity.FormAttributeEntity;
import com.paas.form.entity.FormAttributePubEntity;
import com.paas.form.entity.FormViewEntity;
import com.paas.form.entity.FormViewPubEntity;
import com.paas.form.service.IFormAttributePubService;
import com.paas.form.service.IViewChangeService;
import com.paas.form.service.impl.ViewChangeServiceImpl;
import com.paas.form.vo.FormViewPubChangeVo;
import com.paas.form.vo.HadAttrVo;
import com.paas.form.vo.ViewPublishVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import com.paas.common.model.BaseUserInfo;
import com.paas.common.util.StringUtil;
import com.paas.common.web.SystemConst;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.paas.form.service.IFormViewPubService;
import com.paas.common.util.BeanUtil;
import com.paas.common.web.annotion.OperLog;
import com.paas.common.web.controller.BaseController;
import com.paas.common.web.oper.OprLogConstant;

@RestController
@RequestMapping(value = "/form/formViewPub")
@Api(description = "dd", tags = "")
public class FormViewPubController extends BaseController {

    @Autowired
    public IFormViewPubService formViewPubService;

    @Autowired
    public IFormAttributePubService formAttributePubService;

    @Autowired
    public IViewChangeService viewChangeService;

    @Autowired
    public IBpmProcessInstanceService bpmProcessInstanceService;

    //查询视图名是否已有，视图名唯一校验
    @RequestMapping(value = "/checkViewName", method = RequestMethod.POST)
    @ApiOperation(value = "根据Id查询对象", notes = "根据Id查询对象")
    public RestResponse checkViewName(@RequestBody FormViewPubEntity formViewPubEntity) {
        if(StringUtil.isNotEmpty(formViewPubEntity.getName())){
            try{
                QueryWrapper<FormViewPubEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name",formViewPubEntity.getName());
                FormViewPubEntity bean = formViewPubService.getOne(queryWrapper);
                if(BeanUtil.isEmpty(bean)){
                    return RestResponse.success();
                }else if(StringUtil.isNotEmpty(formViewPubEntity.getId())&&formViewPubEntity.getId().equals(bean.getId())){
                    return RestResponse.success();
                }
            }catch (Exception e){
                e.printStackTrace();
                return RestResponse.error("该视图名称已存在");
            }
        }
        return RestResponse.error("该视图名称已存在");
    }


    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ApiOperation(value = "保存信息", notes = "保存信息")
    @OperLog(operType = OprLogConstant.ADD,operModul = "",operDescribe = "-保存")
    public void save(@RequestBody FormViewPubEntity formViewPubEntity) throws ParseException{
		 BaseUserInfo baseUserInfo = getUserInfo();
        if(StringUtil.isEmpty(formViewPubEntity.getId())){
    		formViewPubEntity.setStatus(SystemConst.NORMAL_STATUS);
			injectUpdateDataAuth(formViewPubEntity);
    	}
		formViewPubEntity.setModifyDate(new Date());
    	formViewPubService.save(formViewPubEntity);
    }



    @RequestMapping(value = "/attrHadCheck", method = RequestMethod.POST)
    @ApiOperation(value = "批量判断已存在属性", notes = "批量判断已存在属性")
    @OperLog(operType = OprLogConstant.UPDATE, operModul = "", operDescribe = "-批量判断已存在属性")
    public List<String> attrHadCheck(@RequestBody HadAttrVo hadAttrVo ) throws ParseException {
        List<String> codeList=hadAttrVo.getCodeList();
        String viewId=hadAttrVo.getViewId();
        if (codeList != null && codeList.size() > 0) {
            FormAttributePubEntity attribute=new FormAttributePubEntity();
            attribute.setViewId(viewId);
            List<FormAttributePubEntity> formAttributeList= formAttributePubService.getList(attribute);

            List<String> hadcodeList =new ArrayList<>();
            if(formAttributeList.size()>0){
                for (int i = 0; i <codeList.size() ; i++) {
                    for (int j = 0; j <formAttributeList.size() ; j++) {
                        if(formAttributeList.get(j).getCode().equals(codeList.get(i))){
                            hadcodeList.add(codeList.get(i));
                        }

                    }



                }
                return hadcodeList;
            }else {
                return null;
            }


        }
        return null;

    }





    @RequestMapping(value = "/saveBatch",method = RequestMethod.POST)
    @ApiOperation(value = "批量保存信息", notes = "批量保存信息")
    @OperLog(operType = OprLogConstant.UPDATE,operModul = "",operDescribe = "-批量保存修改")
    public void saveBatch(@RequestBody List map) throws ParseException{
    	List list = BeanUtil.mapsToList(map, FormViewPubEntity.class);
    	formViewPubService.saveBatch(list);
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    @ApiOperation(value = "根据Id删除数据", notes = "根据Id删除数据")
    @OperLog(operType = OprLogConstant.DELETE,operModul = "",operDescribe = "-删除")
    public void delectById(String id){
		if (null != id && !"".equals(id)) {
			FormViewPubEntity formViewPubEntity = new FormViewPubEntity();
			formViewPubEntity.setId(id);
			injectUpdateDataAuth(formViewPubEntity);
			formViewPubService.deleteByEntity(formViewPubEntity);
		}
    }

    @RequestMapping(value = "/deleteBatchByIds",method = RequestMethod.POST)
    @ApiOperation(value = "根据Id批量删除数据", notes = "根据Id批量删除数据")
    @OperLog(operType = OprLogConstant.DELETE,operModul = "",operDescribe = "-批量删除")
    public void deleteBatchByIds(@RequestBody List ids) {
		formViewPubService.deleteBatchIds(ids);
    }

    @RequestMapping(value = "/queryById",method = RequestMethod.GET)
    @ApiOperation(value = "根据Id查询对象", notes = "根据Id查询对象")
    public FormViewPubEntity queryById(String id){
         FormViewPubEntity bean = formViewPubService.getById(id);
         return bean;
    }

    @RequestMapping(value = "/queryViewDetailById",method = RequestMethod.GET)
    @ApiOperation(value = "查询视图明细", notes = "查询视图明细")
    public ViewPublishVo queryViewDetailById(String id){
        ViewPublishVo bean = formViewPubService.getViewFinalizeById(id);
        return bean;
    }

    @RequestMapping(value = "/queryBatchByIds")
    @ApiOperation(value = "根据Id查询对象列表", notes = "根据Id查询对象列表")
    public List<FormViewPubEntity> queryBatchByIds(@RequestBody List<String> ids){
         List<FormViewPubEntity> list = formViewPubService.getBatchByIds(ids);
         return list ;
    }

    @RequestMapping(value = "/queryPage",method = RequestMethod.GET)
    @ApiOperation(value = "模板分页查询", notes = "模板分页查询")
    public IPage queryPage(FormViewPubEntity formViewPubEntity, Integer currentPage, Integer pageSize){
    	if (currentPage == null) {
    		currentPage = 1;
    	}
    	if (pageSize == null) {
    		pageSize = 10;
    	}
		injectUpdateDataAuth(formViewPubEntity);
        formViewPubEntity.setRootViewId("1");
		IPage<FormViewPubEntity> pageBean = formViewPubService.getPage(formViewPubEntity, currentPage, pageSize);
        List<FormViewPubEntity> list = pageBean.getRecords();
        for (FormViewPubEntity formViewPub : list){
           FormViewPubEntity formViewPub1 = new FormViewPubEntity();
           formViewPub1.setViewId(formViewPub.getId());
            List<FormViewPubEntity> pubEntityList = formViewPubService.getList(formViewPub1);
            if (pubEntityList.size() > 0 ){
                formViewPub.setChildList(pubEntityList);
            }
            pageBean.setRecords(list);
        }
    	return pageBean;
    }
    @RequestMapping(value = "/querytreeData",method = RequestMethod.GET)
    @ApiOperation(value = "模板分页查询", notes = "模板分页查询")
    public List<FormViewPubEntity> querytreeData(FormViewPubEntity formViewPubEntity){
		injectUpdateDataAuth(formViewPubEntity);
        formViewPubEntity.setRootViewId("1");
        List<FormViewPubEntity> list = formViewPubService.getList(formViewPubEntity);
        for (FormViewPubEntity formViewPub : list){
           FormViewPubEntity formViewPub1 = new FormViewPubEntity();
           formViewPub1.setViewId(formViewPub.getId());
            List<FormViewPubEntity> pubEntityList = formViewPubService.getList(formViewPub1);
            if (pubEntityList.size() > 0 ){
                formViewPub.setChildList(pubEntityList);
            }
        }
    	return list;
    }

    @RequestMapping(value = "/queryList",method = RequestMethod.GET)
    @ApiOperation(value = "模板查询", notes = "模板查询")
    public List<FormViewPubEntity> queryList(FormViewPubEntity formViewPubEntity){
        injectUpdateDataAuth(formViewPubEntity);
        List<FormViewPubEntity> list = formViewPubService.getList(formViewPubEntity);
        return list;
    }
}